package com.plant.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.plant.entity.UgcFriends;
import com.plant.entity.UgcFriendsApply;
import com.plant.entity.UgcUser;
import com.plant.service.Friendservic;
import com.plant.service.HelpService;
import com.plant.service.UserServic;
import com.plant.tool.SendMessage;

/**用户好友关系用例
 */
@Controller //注解控制器
@RequestMapping(value = "/addfriend")
public class AddfriendsAction {
	@Resource(name ="friendserviceImpl")//---朋友关系接口方法
	private Friendservic friendservic;

	@Resource(name = "userInfoImlp")  //---用户个人接口方法
	private UserServic userServic;

	@Resource(name = "helpServiceImpl")//--帮助接口方法
	private HelpService helpService;

	/**用户申请加好友
	 */
	@RequestMapping(value="apply")
	@ResponseBody
	public Map<Object,Object> addfriends(UgcUser uf,HttpServletResponse response,UgcFriendsApply apply,
			HttpServletRequest request,String secret,String mdsc){  //1.发送申请加好友请求接口  
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object, Object>();
		if(secret==null||mdsc==null){
			map.put("message", "参数不能为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mds=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mds)){	//通过md5加密】
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String t=sim.format(new Date());
				GetIp getip=new GetIp();
				String ip=getip.getIp2(request);
				apply.setCreateTime(Timestamp.valueOf(t));
				apply.setIp(ip);
				apply.setState(0);
				boolean b=friendservic.addFriends(apply);
				if(b){
					uf.setId(apply.getToUserId());
					UgcUser u=(UgcUser) userServic.OneUser(uf);
					uf.setId(apply.getUserId());
					UgcUser uu=(UgcUser) userServic.OneUser(uf);
					String deviceToken=u.getDiviceToken();
					if(deviceToken==null){
						map.put("sendmes", "好友Token为空不用推送");
					}else{
						String nickname=uu.getNickname();
						SendMessage send=new SendMessage();
						String mes=send.IosSendMessageOne(deviceToken,nickname);
						map.put("sendmes", mes);
					}
					map.put("message", "申请成功");
					map.put("state", 1);
					return map;
				}else{
					map.put("message", "申请失败");
					map.put("state", 0);
					return map;
				}
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}


	/**@param user_id     当前用户id
	 * @param to_user_id  被申请加好友的用户id
	 * @return            处理同意好友请求接口*/
	@RequestMapping(value="deal")
	@ResponseBody
	public Map<String,Object> dealreq(Integer user_id,Integer to_user_id,HttpServletResponse response,UgcFriendsApply apply,HttpServletRequest request,String secret,String mdsc){   //requestbody接受json格式数据
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map = new HashMap<String,Object>(); 
		if(secret==null||mdsc==null||user_id==null||to_user_id==null){
			map.put("message", "参数不能为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mds=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mds)){
				apply.setUserId(user_id);
				apply.setToUserId(to_user_id);
				List<Object> list1=new ArrayList<Object>();
				List<Object> list2=new ArrayList<Object>();
				list1=userServic.isfriend(apply.getUserId(), apply.getToUserId());
				list2=userServic.IsNotfriend(apply.getToUserId(), apply.getUserId());
				if(list1.size()==0&&list2.size()==0){
					boolean bo=friendservic.ValidateFriend(apply);//--//1..验证是否已经是好友了
					if(bo){
						map.put("message", "该用户已经是你的好友");
						map.put("state", 0);
						return map;
					}else{
						GetIp getip=new GetIp();
						String ip=getip.getIp2(request);
						boolean b=friendservic.update(apply);  //---2.调用修改接口修改 同意加好友修改为1 
						if(b){
							UgcFriends uf=new UgcFriends();
							uf.setReceiveUserId(apply.getToUserId());
							uf.setApplyUserId(apply.getUserId());
							SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							String t=sim.format(new Date());
							uf.setCreateTime(Timestamp.valueOf(t));
							uf.setIp(ip);
							friendservic.addfriend(uf);
						}else{
							System.out.println("插入不成功");	
						}
						map.put("state", 1);
						map.put("message", "同意成功");
						return map;
					}
				}else{
					map.put("message", "用户已经是你好友不必继续操作");
					map.put("state", 0);
					return map; 
				}
			}else{
				map.put("state", 0);
				return map;
			}
		}
	}


	@RequestMapping(value ="listmyfriend")//-----3查看我的好友接口
	@ResponseBody
	public Map<Object,Object> MyFriend(Integer user_id,HttpServletResponse response,UgcUser uf,String secret,String pust_val){//---3.查看我的好友接口
		response.setHeader("access-control-allow-origin","*");
		List<Object> list=new ArrayList<Object>();
		Map<Object,Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||user_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				uf.setId(user_id);
				List<Object> list3=new ArrayList<Object>();
				List<Object> list2=new ArrayList<Object>();
				list2=helpService.GetFriendUidByA(uf);
				list3=helpService.GetFriendUidByR(uf);
				list3.addAll(list2);
				int len=list3.size();
				if(len>0){
					String ids=StringUtils.join(list3.toArray(),",");
					list=friendservic.listfriend(ids);
					map.put("message", "查询好友成功");
					map.put("state", 1);
					map.put("content", list);
				}else{
					map.put("message", "没有好友");
					map.put("state", 0);
				}
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}


	@RequestMapping(value ="list_not_myfriend")//--查看不是我的好友
	@ResponseBody
	public Map<Object,Object> NotMyFriend(Integer user_id,HttpServletResponse response,UgcUser uf,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||user_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				//				uf.setId(187);
				//				Integer user_id=187;
				List<Object> list=new ArrayList<Object>();
				uf.setId(user_id);
				List<Object> list3=new ArrayList<Object>();
				List<Object> list2=new ArrayList<Object>();
				list2=helpService.GetFriendUidByA(uf);
				list3=helpService.GetFriendUidByR(uf);
				list3.addAll(list2);
				int len=list3.size();
				if(len==0){
					Object ids=user_id;
					list=friendservic.listnotfriend(ids);
					if(list.size()>0){
						map.put("message", "查询成功");
						map.put("state", 1);
						map.put("content", list);
					}else{
						map.put("message", "查询失败");
						map.put("state", 0);
						map.put("content", list);
					}
				}else{
					list3.add(user_id);
					Object ids=StringUtils.join(list3.toArray(),",");
					list=friendservic.listnotfriend(ids);
					if(list.size()>0){
						map.put("message", "查询好友成功");
						map.put("state", 1);
						map.put("content", list);
					}else{
						map.put("message", "查询好友失败");
						map.put("state", 0);
						map.put("content", list);
					}
				}
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}


	@RequestMapping(value ="delmyfriend")  //----4.删除我的好友
	@ResponseBody
	public Map<Object, Object> delmyfriend(HttpServletResponse response,UgcFriends uf,String secret,String mdsc){   //---3.删除我的好友
		response.setHeader("access-control-allow-origin","*");
		Map<Object, Object> map=new HashMap<Object, Object>();
		if(uf.getReceiveUserId()==null||secret==null||mdsc==null||uf.getApplyUserId()==null){
			map.put("message", "参数错误");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mdc)){
				boolean b=friendservic.delfriend(uf);
				if(b){
					map.put("message", "删除成功");
					map.put("state", 1);
					return map;
				}else{
					map.put("message", "删除失败");
					map.put("state", 0);
					return map;
				}
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}


	@RequestMapping(value ="ignorefriend")
	@ResponseBody
	public Map<Object,Object> ignorefriend(HttpServletResponse response,UgcFriendsApply apply,String secret,String mdsc){//4--忽略好友请求接口
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
		if(apply.getId()==null||secret==null||mdsc==null){
			map.put("error", "参数不能为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mdc)){
				boolean b=friendservic.Ignoreupdate(apply);//忽略好友修改为2
				if(b){
					map.put("state", 1);
					map.put("message", "忽略好友请求成功");
					return map;
				}else{
					map.put("state", 0);
					map.put("message", "忽略好友请求失败");
					return map;
				}
			}else{
				map.put("error", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}

}
